<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/9/25
 * Time: 13:22
 */

namespace app\admin\controller;

use app\admin\service\ConfigService;
use app\models\DictionaryModel;
use app\models\TypeModel;

/**
 * Class Config
 * @package app\admin\controller
 */
class Config extends Common
{
    /**
     * 字典管理列表
     * @return \think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function dictionary()
    {
        if (request()->isPost()) {
            return json((new ConfigService())->dictionary());
        }
        $dictionaryModel = new DictionaryModel();
        $types = $dictionaryModel
            ->where(['status' => 1])
            ->field('type,type_name')
            ->group('type,type_name')
            ->select();
        $this->assign('types', $types);
        return view();
    }

    /**
     * 新增字典
     * @return \think\response\Json|\think\response\View
     * @throws \think\exception\DbException
     */
    public function add()
    {
        if (request()->isPost()) {
            return json((new ConfigService())->add());
        }
        $types = TypeModel::all();
        $this->assign('types', $types);
        return view();
    }

    /**
     * 编辑字典
     * @return \think\response\Json|\think\response\View
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        if (request()->isPost()) {
            return json((new ConfigService())->add());
        }
        $types = TypeModel::all();
        $this->assign('types', $types);
        $config = DictionaryModel::get(input('id'));
        $this->assign('config', $config);
        return view();
    }

	/**
	 * 字典类型
	 * modify by Small Y
	 *
	 * @return \think\response\Json|\think\response\View
	 */
    public function type()
    {
        if (request()->isPost()) {
            return json((new ConfigService())->type());
        }
        return view();
    }

	/**
	 * 新增字典类型
	 * modify by Small Y
	 *
	 * @return \think\response\Json
	 */
    public function type_add()
    {
        if (request()->isPost()) {
            return json((new ConfigService())->type_add());
        }
    }

    /**
     * 改变排序
     */
    public function change_sort()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $typeModel = new TypeModel();
            $url = '';
            if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) {
                $url = $_SERVER["HTTP_REFERER"];
            } elseif ('' !== $url) {
                $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : Url::build($url);
            }
            if ($typeModel->where('id',input('id'))->update($data) !== false) {
                return ['code' => 0, 'msg' => "更新成功", 'url' =>$url];
            }
            return ['code' => 1, 'msg' => "更新失败"];
        }
    }

	/**
	 * User: wufeng
	 * Date: 2020/03/31
	 *
	 */
	public function check_status()
	{
		if ($this->request->isAjax() && $this->request->isPost()) {
			$data = input('post.');
			if (1 === (int) $data['status']) {
				$msg = '启用';
			} else {
				$msg = '禁用';
			}

			$res = (new DictionaryModel())->save($data, ['id' => $data['id']]);

			if (false !== $res) {
				$this->success($msg.'成功', '', '', 1);
			}
			$this->error($msg.'失败', null, '', 1);
		}
	}
}